<template>
  <div v-if="$special">
    <el-button type="success" @click="visible = true" class="el-icon-download">导出PDF</el-button>
    <el-dialog
        title="导出PDF"
        width="600px"
        @close="cancel"
        :close-on-click-modal="false"
        :visible.sync="visible">
      <el-form :model="form" :rules="rules" label-width="100px" ref="Form">
        <el-form-item label="日期:" prop="beginTime">
          <date-time-picker :start.sync="form.beginTime" :end.sync="form.endTime"/>
        </el-form-item>
        <el-form-item label="状态:">
          <span>揽收</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <submit-button ref="Submit" text="导 出" @submit="submit"/>
      </div>
    </el-dialog>
    <print-more ref="PrintMore"/>
  </div>
</template>

<script>
  import {listSpecialExpressOrderApi} from '@/api/export';
  import {resetForm} from "@/utils/common";
  import DateTimePicker from '@/components/DateTimePicker'
  import PrintMore from '@/views/print/print_more'

  export default {
    name: 'ExportPDF',
    components: {DateTimePicker, PrintMore},
    data() {
      return {
        isLoading: false,
        visible: false,
        form: {
          beginTime: '',
          endTime: '',
          state: 0
        },
        rules: {
          beginTime: {required: true, message: '请选择日期', trigger: 'change'}
        }
      }
    },
    methods: {
      submit() {
        this.$refs['Form'].validate(valid => {
          if (!valid) return false;
          this.$refs.Submit.start()
          let param = {...this.form}
          if (!param.beginTime) delete param.beginTime;
          if (!param.endTime) delete param.endTime;
          listSpecialExpressOrderApi(param)
            .then(result => {
              this.$refs.Submit.stop()
              let response = result.resultParam.expressOrderList;
              let list = response.map(item => item.number)
              this.printMore(list)
              this.cancel();
            })
            .catch(() => {
              this.$refs.Submit.stop()
            })
        });
      },
      // 批量打印
      printMore(list) {
        let _this = this.$refs.PrintMore;
        _this.visible = true;
        _this.numberList = list;
        _this.getPrint();
      },
      cancel() {
        resetForm(this)
      }
    }
  }
</script>
